package com.wsj.util.tool;

import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger; 
import org.slf4j.LoggerFactory; 

public class TestString { 

    private final Logger logger = LoggerFactory.getLogger(this.getClass()); 

     
    public void testPlus() { 
        String s = ""; 
        long ts = System.currentTimeMillis(); 
        for (int i = 0; i < 10000; i++) { 
            s = s + String.valueOf(i); 
        } 
        long te = System.currentTimeMillis(); 
        logger.info("+ cost {} ms", te - ts); 
    } 

     
    public void testConcat() { 
        String s = ""; 
        long ts = System.currentTimeMillis(); 
        for (int i = 0; i < 10000; i++) { 
            s = s.concat(String.valueOf(i)); 
        } 
        long te = System.currentTimeMillis(); 
        logger.info("concat cost {} ms", te - ts); 
    } 

     
    public void testJoin() { 
        Object[] obj = new Object[]{};
        long ts = System.currentTimeMillis(); 
        for (int i = 0; i < 10000; i++) { 
            obj[i] = String.valueOf(i);
        	//list.add(String.valueOf(i)); 
        } 
        StringUtils.join(obj, "");
        //StringUtils.join(list, ""); 
        long te = System.currentTimeMillis(); 
        logger.info("StringUtils.join cost {} ms", te - ts); 
    } 

     
    public void testStringBuffer() { 
        StringBuffer sb = new StringBuffer(); 
        long ts = System.currentTimeMillis(); 
        for (int i = 0; i < 10000; i++) { 
            sb.append(String.valueOf(i)); 
        } 
        sb.toString(); 
        long te = System.currentTimeMillis(); 
        logger.info("StringBuffer cost {} ms", te - ts); 
    } 


    public void testStringBuilder() { 
        StringBuilder sb = new StringBuilder(); 
        long ts = System.currentTimeMillis(); 
        for (int i = 0; i < 100000; i++) { 
            sb.append(String.valueOf(i)); 
        } 
        sb.toString(); 
        long te = System.currentTimeMillis(); 
        logger.info("StringBuilder cost {} ms", te - ts); 
    } 
} 
